<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category_model extends My_Model
{    
    public function __construct()
    {
        parent::__construct();
        $this->model_name="产品库分类";
        $this->tablename="category";  
    }
        
    public function getpage($page,$key=""){
        
        $where=array("delete"=>0);
        if($this->adminid && !$this->ion_auth_acl->has_permission("allshop"))
        {
            $where['adminid']=$this->adminid;
        }

        return $this->get_items_by_page($where,$page,"backend/products","sort desc,id desc",[],$key);                
    }

    public function getadminlist()
    {
        $query=$this->db->select('adminid')->where(["delete"=>0])->group_by('adminid')->get($this->table_prefix.$this->tablename);
        $items=$query->result_array();
        return $items;
    }

    public function getall(){
        
        $where=array("delete"=>0);
        if($this->adminid && !$this->ion_auth_acl->has_permission("allshop"))
        {
            $where['adminid']=$this->adminid;
        }

        return $this->get_items_by_where($where);                
    }
    public function delete($id)
    {
        $this->update($id,["delete"=>1]);
        return true;
    }

    public function getitembyid($id){
        $where=array("delete"=>0,"id"=>$id);
        if($this->adminid && !$this->ion_auth_acl->has_permission("allshop"))
        {
            $where['adminid']=$this->adminid;
        }
        return $this->get_item_by_where($where);
    }

    public function get_item_by_key($key)
    {
        $where=array("delete"=>0,"key"=>$key);
        if($this->adminid && !$this->ion_auth_acl->has_permission("allshop"))
        {
            $where['adminid']=$this->adminid;
        }
        return $this->get_item_by_where($where);
    }
    
    public function create($data){
        if(!isset($data["addtime"])){
            $data["addtime"]=date("Y-m-d H:i:s");
        }
        if(!isset($data["adminid"])||empty($data["adminid"])){
            $data["adminid"]=$this->adminid;
        }
        $data['key']=$this->get_code();
        return $this->add_item($data);       
    }

    public function update($id,$data){

        if(is_array($id))
        {
            $where="id in (".implode(",",$id).")";
            if($this->adminid && !$this->ion_auth_acl->has_permission("allshop"))
            {
                $where.=" and adminid=".$this->adminid;
            }
        }else{
            $where=array("id"=>$id);
            if($this->adminid && !$this->ion_auth_acl->has_permission("allshop"))
            {
                $where['adminid']=$this->adminid;
            }
        }
        return $this->update_item($where,$data);

    }

    private function get_code($length=20)
    {     
        $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $checkstr='';
        $len=strlen($str)-1;
        for ($i=0;$i<$length;$i++)
        {
            $num=mt_rand(0,$len);
            $checkstr.=$str[$num];                  
        }
        return $checkstr;
    }
}